from sklearn.datasets import load_svmlight_file
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import precision_recall_curve
from sklearn.metrics import average_precision_score
from sklearn.metrics import recall_score

num_classes = 10

def load_to_numpy(path):
    data = load_svmlight_file(path)
    return (data[0].toarray(), data[1])

(x_test, y_test) = load_to_numpy("usps.t")

x_test = x_test.reshape(x_test.shape[0], 16, 16, 1)
y_test = y_test - 1
y_test = keras.utils.to_categorical(y_test, num_classes)
model = keras.models.load_model('usps.h5')
y_score = model.predict(x_test)
y_class = np.argmax(y_score, axis=1)
y_class = keras.utils.to_categorical(y_class, num_classes)

precision = dict()
recall = dict()
average_precision = dict()

for i in range(num_classes):
    precision[i], recall[i], _ = precision_recall_curve(y_test[:, i], y_score[:, i])
    average_precision[i] = average_precision_score(y_test[:, i], y_score[:, i])
    average_recall = recall_score(y_test[:, i], y_class[:, i])
    print("-------------------------------------")
    print("数字", i)
    print("Precision: %.4f" % average_precision[i])
    print("Recall: %.4f" % average_recall)

precision["micro"], recall["micro"], _ = precision_recall_curve(y_test.ravel(), y_score.ravel())
average_precision["micro"] = average_precision_score(y_test, y_score, average="micro")

plt.figure(figsize=(20, 8), dpi=80)
plt.step(recall['micro'], precision['micro'], where='post')
plt.xlabel('Recall')
plt.ylabel('Precision')
plt.ylim([0.0, 1.05])
plt.xlim([0.0, 1.0])
plt.show()
